<?php

include_once 'Classes/PHPCodeProvider.php';

$options = new CodeGeneratorOptions();
$writer = new IndentedTextWriter();
$provider = new PHPCodeProvider();

/*
 * Reference an associative array element
 */

$indexer1 = new ArrayIndexerExpression();
$indexer1->TargetObject = new VariableReferenceExpression('score');
$indexer1->Indices->add(new PrimitiveExpression('won'));

$provider->generateCodeFromExpression($indexer1, $writer, $options);

echo '<pre>' . 
    str_replace('<br />', '', highlight_string($writer->get_String(), true)) 
        . '<pre/>';

$writer->clear();

/*
 * Reference a one-dimensional array element
 */

$indexer2 = new ArrayIndexerExpression();
$indexer2->TargetObject = new VariableReferenceExpression('myArray');
$indexer2->Indices->add(new VariableReferenceExpression('i'));

$provider->generateCodeFromExpression($indexer2, $writer, $options);

echo '<pre>' . 
    str_replace('<br />', '', highlight_string($writer->get_String(), true)) 
        . '<pre/>';

$writer->clear();

/*
 * Reference a two-dimensional array element
 */

$indexer3 = new ArrayIndexerExpression();
$indexer3->TargetObject = new VariableReferenceExpression('myArray');
$indexer3->Indices->add(new VariableReferenceExpression('i'));
$indexer3->Indices->add(new BinaryOperatorExpression(
    BinaryOperator::ADD, 
    new VariableReferenceExpression('j'), 
    new PrimitiveExpression(2)));

$provider->generateCodeFromExpression($indexer3, $writer, $options);

echo '<pre>' . 
    str_replace('<br />', '', highlight_string($writer->get_String(), true)) 
        . '<pre/>';

/*
 * The output should be:
 * 
 * $score['won']
 *
 * $myArray[$i]

 * $myArray[$i, $j + 2]

 * 
 */

?>
